package com.si.sea_island;



import com.si.sea_island.base.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

//@RestControllerAdvice
@Slf4j
public class JsonResultWrapper implements ResponseBodyAdvice<JsonResult> {

    @Override
    public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
        // 判断是否支持拦截处理该类型的返回值，这里拦截的是 JsonResult 类型
        return returnType.getParameterType().equals(JsonResult.class);
    }

    @Override
    public JsonResult beforeBodyWrite(JsonResult body, MethodParameter returnType, MediaType selectedContentType,
                                      Class<? extends HttpMessageConverter<?>> selectedConverterType,
                                      ServerHttpRequest request, ServerHttpResponse response) {
        String reqUrl = request.getURI().toString();
        // 对 JsonResult 对象进行处理，例如修改属性值等
        log.info(" 请求地址："+reqUrl + "返回值：{}",body.toString());
        return body;
    }
}